﻿using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace UI
{
    public static class UIUtility
    {
        
    }
    public static class UIExtensions
    {
        /// <summary>
        /// 获取滚动列表定位到target的VerticalPosition
        /// </summary>
        public static float GetVerticalPosition(this ScrollRect scrollRect, RectTransform target, float yOffset = 0f)
        {
            var rate = Mathf.Clamp01((Mathf.Abs(target.localPosition.y) + yOffset) / //获取target的上顶点
                                     (scrollRect.content.rect.height - scrollRect.viewport.rect.height));
            return 1 - rate;
        }
        /// <summary>
        /// 让滚动列表定位到target
        /// </summary>
        public static void SetVerticalPosition(this ScrollRect scrollRect, RectTransform target, float yOffset = 0f)
        {
            scrollRect.verticalNormalizedPosition = GetVerticalPosition(scrollRect, target, yOffset);
        }
        /// <summary>
        /// 获取滚动列表定位到target的HorizontalPosition
        /// </summary>
        public static float GetHorizontalPosition(this ScrollRect scrollRect, RectTransform target, float xOffset = 0f)
        {
            var rate = Mathf.Clamp01((Mathf.Abs(target.localPosition.x) + xOffset) / //获取target的左顶点 
                                     (scrollRect.content.rect.width - scrollRect.viewport.rect.width));
            return rate;
        }
        /// <summary>
        /// 让滚动列表定位到target
        /// </summary>
        public static void SetHorizontalPosition(this ScrollRect scrollRect, RectTransform target, float xOffset = 0f)
        {
            scrollRect.horizontalNormalizedPosition = GetHorizontalPosition(scrollRect, target, xOffset);
        }
        
        /// <summary>
        /// 设置到最佳宽度
        /// </summary>
        public static float SetPreferredWidth(this TextMeshProUGUI text, float offset = 0f)
        {
            var sizeDelta = text.rectTransform.sizeDelta;
            sizeDelta.x = text.preferredWidth + offset;
            text.rectTransform.sizeDelta = sizeDelta;
            return sizeDelta.x;
        }
        /// <summary>
        /// 设置到最佳高度
        /// </summary>
        public static float SetPreferredHeight(this TextMeshProUGUI text, float offset = 0f)
        {
            var sizeDelta = text.rectTransform.sizeDelta;
            sizeDelta.y = text.preferredHeight + offset;
            text.rectTransform.sizeDelta = sizeDelta;
            return sizeDelta.y;
        }

        /// <summary>
        /// 刷新布局到最适合的大小
        /// </summary>
        public static float RefreshSizeToPreferred(this LayoutGroup layout, RectTransform.Axis axis, float extraSize = 0f, float minSize = 0f)
        {
            var layoutRoot = layout.transform as RectTransform;
            
            layout.enabled = true;
            LayoutRebuilder.ForceRebuildLayoutImmediate(layoutRoot);
            float size;
            if (axis == RectTransform.Axis.Horizontal)
            {
                layout.SetLayoutHorizontal();
                size = layout.preferredWidth;
            }
            else
            {
                layout.SetLayoutVertical();
                size = layout.preferredHeight;
            }
            size = Mathf.Max(size, minSize);
            layoutRoot.SetSizeWithCurrentAnchors(axis, size + extraSize);
            if (axis == RectTransform.Axis.Horizontal)
            {
                layout.SetLayoutHorizontal();
            }
            else
            {
                layout.SetLayoutVertical();
            }
            layout.enabled = false;
            return size;
        }
        /// <summary>
        /// 刷新布局
        /// </summary>
        public static void RefreshLayout(this LayoutGroup layout)
        {
            var layoutRoot = layout.transform as RectTransform;
            layout.enabled = true;
            LayoutRebuilder.ForceRebuildLayoutImmediate(layoutRoot);
            layout.enabled = false;
        }
    }
}